<?php
namespace Home\Controller;
use Think\Controller;
class FormController extends Controller{
	public function index(){
	//echo '1234';
	}
	//添加新闻
	public function insert(){
		$upload = upload_img();
		$info = $upload->upload();
		if(!$info){
			$this->error($upload->getError());
		}else{
			$form = D('news');
			
			if($_SERVER["REQUEST_METHOD"] == "POST"){
				
				$data['title'] = test_input(I('param.title'));
				$data['content'] = test_input(I('content'));
				$data['eauthor'] = test_input(I('eauthor'));
				$data['pic'] = $info['pic']['savepath'].$info['pic']['savename'];	

				$check_date = check_date(I('etime'));
				if($check_date){
					$data['etime'] = I('etime');
				}else{
					$data['etime'] = date("Y-m-d H:i:s",time());
				}
				
				//插入数据库
				if($form->create()){
					$result = $form->data($data)->add();
					if($result){
						$this->success('添加新闻成功，即将返回列表页面','../');
					}else{
						$this->error('添加新闻失败');
					}
				}else{
					$this->error($form->getError());
				}
				
			}else{
				$this->error('不是post提交的数据');
			}
			
		}
	}
	
	//读取新闻详情
	public function read($id=0){
		$form = M('news');
		$data = $form->find($id);
		if($data){
			$this->assign('data',$data);
		}else{
			$this->error();
		}
		
		$this->display();
		
	}
	
	
	//编辑新闻并更新到数据库
	public function edit($id=0){
		$form = M('news');
		$this->assign('vo',$form->find($id));
		$this->display();
	}
	
	public function update(){
		
		$upload = upload_img();
		$info = $upload->upload();
		if(!$info){
			$this->error($upload->getError());
		}else{
			$form = D('news');
			
			if($_SERVER["REQUEST_METHOD"] == "POST"){
				
				$data['id'] = $_POST['id'];
				$data['title'] = test_input($_POST['title']);
				$data['content'] = test_input($_POST['content']);
				$data['eauthor'] = test_input($_POST['eauthor']);
				$data['pic'] = $info['pic']['savepath'].$info['pic']['savename'];
				
				$check_date = check_date($_POST['etime']);
				if($check_date){
					$data['etime'] = $_POST['etime'];
				}else{
					$data['etime'] = date("Y-m-d H:i:s",time());
				}
				
				if($form->create()){
					$result = $form->data($data)->save();
					
					if($result){
						$this->success('修改新闻成功,即将跳转到详情页面',U('Form/read?id='.$data[id]));
					}else{
						$this->error('修改新闻信息失败，请重试');
					}
					
				}else{
					$this->error($form->getError());
				}
				
			}else{
				$this->error('不是post提交的数据');
			}
			
		}
		
	}
	
	//删除数据
	public function delete($id=0){
		$Form = M('news');
		$result = $Form->delete($id);
		if($result){
			$this->success('删除新闻成功!','/newskk_v3/');
		}else{
			$this->error('删除新闻信息失败！');
		}
	}
	
}
?>





























